package leetcode101.divide_conquer;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Synhard
 * @version 1.0
 * @Class leetcode101.graph.Code1
 * @Description 241. 为运算表达式设计优先级
 * 给定一个含有数字和运算符的字符串，为表达式添加括号，改变其运算优先级以求出不同的结果。
 * 你需要给出所有可能的组合的结果。有效的运算符号包含 +, - 以及 * 。
 *
 * 示例 1:
 *
 * 输入: "2-1-1"
 * 输出: [0, 2]
 * 解释: 
 * ((2-1)-1) = 0 
 * (2-(1-1)) = 2
 * 示例 2:
 *
 * 输入: "2*3-4*5"
 * 输出: [-34, -14, -10, -10, 10]
 * 解释: 
 * (2*(3-(4*5))) = -34 
 * ((2*3)-(4*5)) = -14 
 * ((2*(3-4))*5) = -10 
 * (2*((3-4)*5)) = -10 
 * (((2*3)-4)*5) = 10
 * @tel 13001321080
 * @email 823436512@qq.com
 * @date 2021-04-23 8:36
 */
public class Code1 {
    public static void main(String[] args) {
        String expression = "2-1-1";
        List<Integer> integers = diffWaysToCompute(expression);
        System.out.println(integers);
    }
    public static List<Integer> diffWaysToCompute(String expression) {
        if (expression == null || expression.length() <= 0) {
            return new ArrayList<Integer>();
        }
        char temp;
        List<Integer> left, right, ans = new ArrayList<>();
        for (int i = 0; i < expression.length(); i++) {
            temp = expression.charAt(i);
            if (temp == '+' || temp == '-' || temp == '*') {
                left = diffWaysToCompute(expression.substring(0, i));
                right = diffWaysToCompute(expression.substring(i + 1));
                for (int l : left) {
                    for (int r : right) {
                        switch (temp) {
                            case '+':
                                ans.add(l + r);
                                break;
                            case '-':
                                ans.add(l - r);
                                break;
                            case '*':
                                ans.add(l * r);
                                break;
                        }
                    }
                }
            }
        }
        if (ans.size() == 0) {
            ans.add(Integer.valueOf(expression));
        }
        return ans;
    }

}
